/**
 * @filename:ModelConfigServiceImpl 2025-05-14
 * @project starhub-schema  V1.0
 */
package com.starhub.engine.model.service.impl;

import com.starhub.application.interfaces.model.ModelConfigService;
import com.starhub.common.bean.BrowserBean;
import com.starhub.common.bean.model.ModelConfig;
import com.starhub.common.sys.user.LoginUser;
import com.starhub.engine.generator.genid.IdGenerator;
import com.starhub.engine.model.dao.ModelConfigDao;
import com.starhub.utils.ColorUtil;
import com.starhub.utils.Util;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.*;
import java.util.stream.Collectors;

/**   
 * @Description:(大模型设置服务实现)
 *
 * @version: V1.0
 * @author: luosw
 * 
 */
@Service
public class ModelConfigServiceImpl implements ModelConfigService {
    @Autowired
    private ModelConfigDao modelDao;

    /**
    * 新增
    * @param user
    * @param model
    * @return
    */
    public Long add(LoginUser user, ModelConfig model){

        Long id = IdGenerator.generateId();

        //数据封装，默认值添加
        model.setId(id);
        model.setCreateTime(new Date());
        model.setUpdateTime(new Date());
        model.setDeleteType(0);
        modelDao.insertSelective(model);
        return id;
    }

    /**
    * 更新
    * @param user
    * @param model
    * @return
    */
    public ModelConfig update(LoginUser user, ModelConfig model){
        model.setUpdateTime(new Date());
        //不允许更新的属性设置为null
        modelDao.updateByPrimaryKeySelective(model);
        return model;
    }

    /**
    * 基本信息获取
    * @param user
    * @param id
    * @return
    */
    public ModelConfig getInfo(LoginUser user, Long id){

        ModelConfig model = modelDao.selectByPrimaryKey(id);
        return model;
    }


    @Override
    public ModelConfig getModel(LoginUser user, String mark) {
        ModelConfig model = modelDao.selectByMark(mark);
        return model;
    }

    @Override
    public ModelConfig getModel(String mark) {
        ModelConfig model = modelDao.selectByMark(mark);
        return model;
    }
    
    /**
    * (批量)删除，同时支持单条或者批量,此方法为逻辑删除
    * @param user
    * @param ids
    */
    public void deleteByType(LoginUser user, String ids,int deleteType){

        //数组转换
        List<Long> idList = Arrays.stream(ids.split(","))
            .map((id) -> Long.parseLong(id))
            .collect(Collectors.toList());
            //删除状态，目前只有1的逻辑
        modelDao.deleteModelConfigByType(idList, deleteType);
    }

    /**
    * 根据id删除，此方法为真实删除
    * @param user
    * @param ids
    */
    public void deleteRecord(LoginUser user, String ids){

        //数组转换
        List<Long> idList = Arrays.stream(ids.split(","))
            .map((id) -> Long.parseLong(id))
            .collect(Collectors.toList());
        modelDao.deleteModelConfigRecord(idList);
    }

    /**
    * 列表查询
    * @param user
    * @param params
    */
    public List<ModelConfig> getList(LoginUser user,  Map<String , Object>  params){

        List<ModelConfig> modelList =  new ArrayList<ModelConfig>();
            //TODO
        int pageSize = 10;
        int current = 1;
        ModelConfig record = new ModelConfig();
        record.setDeleteType(0);
        
        String modelType = Util.null2String(params.get("modelType"));
        if(!Util.isEmpty(modelType)){
            record.setModelType(modelType);
        }
        modelList = modelDao.selectByPrimaryKeySelective(record);
        return modelList;
    }

    @Override
    public Map<String, BrowserBean> getActiveModel(LoginUser user, Map<String, Object> params) {

        Map<String, BrowserBean> result = new HashMap<>();
        ModelConfig record = new ModelConfig();
        record.setDeleteType(0);
        List<ModelConfig> modelList = modelDao.selectByPrimaryKeySelective(record);
        // 查询所有菜单
        List<String> dColors = ColorUtil.dColors;
        int i =0 ;
        if(CollectionUtils.isNotEmpty(modelList)){
            for(ModelConfig model : modelList){
                int co = i % dColors.size();
                i++;
                String color = dColors.get(co);
                BrowserBean bean = new BrowserBean(Util.null2String(model.getMark()) ,model.getName(),color);
                result.put(Util.null2String(model.getId()),bean);
            }
        }
        return result;
    }


}